package com.zfjt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.ClientmessageBean;
import com.zfjt.entity.OrderBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.packnum.PackNum00037;
import com.zfjt.service.ClientmessageBeanService;
import com.zfjt.utils.StringUtil;

/**
 * 查询订单
 * @author xk
 * @version 1.0
 * @date 2017年4月25日
 */
@Controller
public class InquiryOrderController {
	
	private ReturnBean mReturn;
	
	@Autowired
	private ClientmessageBeanService mClientMessage;
	
	private Map<String, String> fhdata; 
	
	private OrderBean fh;
	
	private ClientmessageBean fhdx;
	
	@RequestMapping(value="00037")
	@ResponseBody
	public ReturnBean AddBrabch(HttpSession hseesion,HttpServletRequest request){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00037 num00037 = JSON.parseObject(data,PackNum00037.class); 	
		List<ClientmessageBean> mData = mClientMessage.selectAll();
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00037");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{
			int is = 0;
			for (int i = 0; i < mData.size(); i++) {
				ClientmessageBean dg = mData.get(i); 
				if (dg.getMobile().equals(num00037.getMobile())) {
					fhdx = dg;
					is = 1;
					String [] str = StringUtil.convertStrToArray(dg.getName());
					fh = new OrderBean(dg.getId(),str[0], dg.getMobile(), dg.getType(),
							dg.getAddtime(),dg.getMoney(),dg.getCharge(),dg.getRejectImg(),dg.getProduct(),dg.getGenre(),dg.getAffiliationWangdian());
					break;
				}
			}
			if (is == 1) {
				if (fh.getType().equals("1")) {		//未完成订单
					mReturn.setCode("002");
					mReturn.setPortnum("00037");
					fhdata.put("error", "TYPE1");
					mReturn.setData(JSON.toJSONString(fhdata));
				}else if (fh.getType().equals("2")) { //待审核订单
					mReturn.setCode("002");
					mReturn.setPortnum("00037");
					fhdata.put("error", "TYPE2");
					mReturn.setData(JSON.toJSONString(fhdata));
				}else if (fh.getType().equals("5")) {//订单已经拒绝
					mReturn.setCode("002");
					mReturn.setPortnum("00037");
					fhdata.put("error", "TYPE5");
					mReturn.setData(JSON.toJSONString(fhdata));
				}else if(num00037.getBrabch().equals("2")){
					if (fh.getBrabch().equals("2")) {
						mReturn.setCode("000");
						mReturn.setPortnum("00037");
						mReturn.setData(JSON.toJSONString(fh));
					}else {								//代表是神州的要输入周坊的订单
						mReturn.setCode("002");
						mReturn.setPortnum("00037");
						fhdata.put("error", "BRABCH_ERROR");
						mReturn.setData(JSON.toJSONString(fhdata));
					}
				}else if(StringUtil.isNotEmpty(fhdx.getPasstime())){//表示这个订单已经输入放款金额了，查询不到
					mReturn.setCode("002");
					mReturn.setPortnum("00037");
					fhdata.put("error", "PASS");
					mReturn.setData(JSON.toJSONString(fhdata));
				}else {
					mReturn.setCode("000");
					mReturn.setPortnum("00037");
					mReturn.setData(JSON.toJSONString(fh));
				}
			}else {
				mReturn.setCode("002");
				mReturn.setPortnum("00037");
				fhdata.put("error", "ABSENCE");
				mReturn.setData(JSON.toJSONString(fhdata));
			}
		}
		return mReturn;
	}
}
